package com.szzg10.mina2.chapter9;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoder;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;

public class ImageRequestEncoder implements ProtocolEncoder {

	@Override
	public void encode(IoSession session, Object message, ProtocolEncoderOutput out)
			throws Exception {
		
		ImageRequest imageRequest = (ImageRequest)message;
		int width = imageRequest.getWidth();
		int height = imageRequest.getHeight();
		int number = imageRequest.getNumber();
		
		IoBuffer ioBuffer = IoBuffer.allocate(12, false);
		ioBuffer.putInt(width);
		ioBuffer.putInt(height);
		ioBuffer.putInt(number);
		
		ioBuffer.flip();
		out.write(ioBuffer);
	}

	@Override
	public void dispose(IoSession session) throws Exception {}

}
